﻿using System.ComponentModel;
using System.Linq;
using System.Windows;
using Fayshine.VA.Helpers;
using Fayshine.VA.RIAService.Web;
using Fayshine.VA.Controls;
using System.Collections.Generic;
using System;

namespace Fayshine.VA.ViewModels
{
    public class POSViewModel : INotifyPropertyChanged
    {
        #region Private Fields

        private FayshineDomainContext m_Context = null;

        #endregion

        public POSViewModel()
        {
            this.PosData = new PagedObservableCollectionView<pos>();
            this.Areas = new PagedObservableCollectionView<area>();
            m_Context = new FayshineDomainContext();
        }

        #region Properties

        public PagedObservableCollectionView<pos> PosData
        {
            get;
            set;
        }

        public PagedObservableCollectionView<area> Areas
        {
            get;
            set;
        }

        #endregion

        #region Public Methods

        public void GetAllAreas()
        {
            var getShopsQuery = m_Context.GetAreasQuery();
            var loadShopsOperation = m_Context.Load(getShopsQuery, (s) =>
            {
                if (!s.HasError)
                {
                   
                    s.Entities.ToList().ForEach(e =>
                    {
                       
                        this.Areas.Add(e);
                    });
                }
                else
                {
                    MessageBox.Show(s.Error.Message + s.Error.StackTrace);
                }
                BusyIndicator indicator = Application.Current.RootVisual as BusyIndicator;
                indicator.IsBusy = false;
            }, true);
        }

        public void GetAllPOSs()
        {
            var getPosQuery = m_Context.GetPosQuery();
            var loadDevicesOperation = m_Context.Load(getPosQuery, (s) =>
            {
                if (!s.HasError)
                {
                    s.Entities.ToList().ForEach(e =>
                    {
                        this.PosData.Add(e);
                    });
                }
                else
                {
                    MessageBox.Show(s.Error.Message + s.Error.StackTrace);
                }
                BusyIndicator indicator = Application.Current.RootVisual as BusyIndicator;
                indicator.IsBusy = false;
            }, true);
        }

        public void GetPosDate()
        { 
           
        }

        public void AddPos(pos pos)
        {
            this.PosData.Add(pos);
            this.m_Context.pos.Add(pos);
            SaveData();
        }

        public void Update(pos pos)
        {
            SaveData();
        }

        public void DeletePOS(pos pos)
        {
            this.PosData.Remove(pos);
            this.m_Context.pos.Remove(pos);
            SaveData();
        }

        public void DeletePOSs(List<pos> poss)
        {
            poss.each(u =>
            {
                this.PosData.Remove(u);
                this.m_Context.pos.Remove(u);
            });
            SaveData();
        }

        public void SaveData()
        {
            if (this.m_Context.HasChanges)
            {
                this.m_Context.SubmitChanges((s) =>
                {

                    //BusyIndicator indicator = Application.Current.RootVisual as BusyIndicator;

                    //indicator.IsBusy = false;
                    if (s.HasError)
                    {
                        MessageBox.Show("Error occurs,Save Failed!");
                    }

                }, null);
            }
        }

        public void SaveData2()
        {
            if (this.m_Context.HasChanges)
            {
                this.m_Context.SubmitChanges((s) =>
                {

                    //BusyIndicator indicator = Application.Current.RootVisual as BusyIndicator;

                    //indicator.IsBusy = false;
                    if (s.HasError)
                    {
                        MessageBox.Show("Error occurs,Save Failed!");
                    }
                    else
                    {
                        MessageBox.Show("保存成功！");
                    }

                }, null);
            }
        }

        public void GetPOSByDate(DateTime date,int shopID)
        {
            BusyIndicator indicator = Application.Current.RootVisual as BusyIndicator;
            indicator.IsBusy = true;
            var getPosQuery = m_Context.GetPosByDateQuery(shopID, date);
            var loadDevicesOperation = m_Context.Load(getPosQuery, (s) =>
            {
                if (!s.HasError)
                {
                    this.PosData.Clear();
                    s.Entities.ToList().ForEach(e =>
                    {
                        this.PosData.Add(e);
                    });
                }
                else
                {
                    MessageBox.Show(s.Error.Message + s.Error.StackTrace);
                }
                // BusyIndicator indicator = Application.Current.RootVisual as BusyIndicator;
                indicator.IsBusy = false;
            }, true);
        }

        //public bool ExistDevice(string deviceName)
        //{
        //    return this.PosData.Where(u => u.Equals(deviceName, System.StringComparison.OrdinalIgnoreCase)).Any();
        //}

        #endregion


        #region INotifyPropertyChanged Interface

        public event PropertyChangedEventHandler PropertyChanged;
        public void NofityPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

    }
}
